package com.gene.service.impl;

import com.gene.mapper.UpmsUserMapper;
import com.gene.mapper.UserGeneticcurrencyMapper;
import com.gene.pojo.UpmsUser;
import com.gene.pojo.UpmsUserExample;
import com.gene.pojo.UserGeneticcurrency;
import com.gene.pojo.UserGeneticcurrencyExample;
import com.gene.service.IUserGeneticcurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class UserGeneticcurrencyServiceImpl implements IUserGeneticcurrencyService {
    @Autowired
    private UserGeneticcurrencyMapper userGeneticcurrencyMapper;

    @Autowired
    private UpmsUserMapper upmsUserMapper;

    @Override
    public List<UserGeneticcurrency> query(UserGeneticcurrency userGeneticcurrency) {
        UserGeneticcurrencyExample example=new UserGeneticcurrencyExample();
        UserGeneticcurrencyExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userGeneticcurrency.getUserId());
        example.setOrderByClause("ctime DESC");
        List<UserGeneticcurrency> list = userGeneticcurrencyMapper.selectByExample(example);
        return list;
    }

    @Override
    public boolean add(UserGeneticcurrency userGeneticcurrency) {
        userGeneticcurrency.setCtime(new Date());
        int a=userGeneticcurrencyMapper.insert(userGeneticcurrency);
        if(a==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean edit(UserGeneticcurrency userGeneticcurrency) {
        int a=userGeneticcurrencyMapper.updateByPrimaryKeySelective(userGeneticcurrency);
        if(a==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(UserGeneticcurrency userGeneticcurrency) {
        int a=userGeneticcurrencyMapper.deleteByPrimaryKey(userGeneticcurrency.getGeneticId());
        if(a==1){
            return true;
        }
        return false;
    }

    /**
     * 根据userId 查询基因币的总和
     * @param
     * @return
     */
    @Override
    public Long sum(UserGeneticcurrency userGeneticcurrency) {

        return userGeneticcurrencyMapper.sum(userGeneticcurrency);
    }


    /**
     * 根据用户名查询出该用户是否存在
     */
    @Override
    public UpmsUser queryUsername(String username) {
        UpmsUserExample example=new UpmsUserExample();
        UpmsUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<UpmsUser> list = upmsUserMapper.selectByExample(example);
        if(list.size()==0)return  null;
        return list.get(0);
    }



    /**
     * 基因币转赠接口
     */
    @Override
    @Transactional
    public boolean present(UserGeneticcurrency userGeneticcurrency) {
        //先判断count是否超出范围
        Long sum = userGeneticcurrencyMapper.sum(userGeneticcurrency);
        if(null == sum) sum = Long.valueOf(0);
        if(Math.abs(userGeneticcurrency.getCount())>sum){
            return false;
        }

        UserGeneticcurrency u1=new UserGeneticcurrency();//送出人
        u1.setCtime(new Date());
        u1.setMode((byte) 3);//送出
        u1.setCount(userGeneticcurrency.getCount());
        u1.setUserId(userGeneticcurrency.getUserId());
        u1.setTarget(userGeneticcurrency.getTarget());//接收人id

        UserGeneticcurrency u2=new UserGeneticcurrency();//接收人
        u2.setCtime(new Date());
        u2.setMode((byte) 4);//收入
        u2.setCount( Math.abs(userGeneticcurrency.getCount()));//取绝对值
        u2.setUserId(userGeneticcurrency.getTarget());
        u2.setTarget(userGeneticcurrency.getUserId());

        int a=userGeneticcurrencyMapper.insert(u1);
        int b= userGeneticcurrencyMapper.insert(u2);

        if(a==1 && b==1){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务

        return false;
    }
}
